<template>
	<view class="main">
		<view class="header">
			<view class="logo">
				<image src="@/static/img/logo.png" mode=""></image>
			</view>
			<view class="content">
				<p class="msg">
					请完善信息进行注册
				</p>
				<ul>
					<li>
						<p>手机号</p>
						<input class="right" v-model="data.mobile" type="number" placeholder="请输入手机号" />
					</li>
					<li>
						<p>密码</p>
						<input class="right" v-model="data.password" type="password" placeholder="请输入密码" />
					</li>
					<li class="code">
						<p>验证码</p>
						<view class="right">
							<input v-model="data.code" type="number" placeholder="请输入验证码" />
							<view class="btn" @tap="getCode()">
								<view v-if="isCode" class="countdown">
									<u-count-down :time="60 * 1000" format="ss" @finish="isCode = false"></u-count-down>
									<span>后可重新获取</span>
								</view>
								<span v-else>获取验证码</span>
							</view>
						</view>
					</li>
					<li>
						<p>员工工号（选填）</p>
						<input class="right" v-model="data.account" type="text" placeholder="请填写员工工号（选填）" />
					</li>
				</ul>
			</view>
		</view>
		<view class="fn">
			<view class="confirm btn" @tap="confirm">
				注册
			</view>
			<view class="msg2">
				<u-checkbox-group v-model="checkboxValue1" placement="column">
					<u-checkbox shape="circle" :name="1"></u-checkbox>
				</u-checkbox-group>
				请您阅读并同意<span @tap="routerTo('/pages/my/UserAgreement')">《用户协议》</span>和<span @tap="routerTo('/pages/my/PrivacyAgreement')">《隐私协议》</span>
			</view>
			<view class="msg">
				已有账号？<span @tap="routerTo('/pages/login/login')">去登录</span>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		register,
		send
	} from '@/api/login.js'
	export default {
		data() {
			return {
				isCode: false,
				data: {
					mobile: '',
					password: '',
					code: '',
					account: '',
				},
				checkboxValue1:[]
			}
		},
		onShow() {
			document.querySelector('.uni-page-head-hd').style.display = 'none'
		},
		methods: {
			async getCode() {
				if (this.data.mobile.length == 0) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
				}
				let res = await send({
					mobile: this.data.mobile,
					event: 'register'
				})
				console.log(res)
				if (res.code == 1) {
					this.isCode = true
					uni.showToast({
						title: '验证码获取成功',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async confirm() {
				if (this.checkboxValue1.length == 0) {
					uni.showToast({
						title: '请先同意用户协议和隐私协议',
						icon: 'none'
					});
					return;
				}
				if (this.data.mobile.length == 0) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				} else if (this.data.password.length < 6) {
					uni.showToast({
						title: '密码最少为六位数',
						icon: 'none'
					})
					return
				} else if (this.data.code.length == 0) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}  else {
					let res = await register(this.data)
					if (res.code == 1) {
						uni.showToast({
							title: '注册成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh);
		background-color: #EFEFEF;

		.header {
			padding-top: 35px;
			background-color: #fff;

			.logo {
				display: flex;
				justify-content: center;
				margin-bottom: 30px;

				image {
					width: 90px;
					height: 120px;
				}
			}

			.content {
				.msg {
					text-align: center;
					color: #666666;
					font-size: 15px;
					margin-bottom: 30px;
				}

				ul {
					padding: 0 30px;

					li {
						display: flex;
						justify-content: space-between;
						align-items: center;
						border-top: 1px solid #EFEFEF;
						padding-top: 20px;
						padding-bottom: 20px;

						input {
							font-size: 15px;
						}

						.right {
							width: 55%;
						}
					}

					.code {
						position: relative;

						.right {
							justify-content: right;
							display: flex;
							align-items: center;

							input {
								// width: 35%;
								flex: 1;
								padding-right: 8px;
							}

							.btn {
								padding: 8px 7px;
								color: #fff;
								background-color: #0C80FB;
								font-size: 12px;
								border-radius: 5px;

								.countdown {
									display: flex;
									align-items: center;

									/deep/.u-count-down__text {
										color: #fff;
										font-size: 12px;
										line-height: 0;
									}
								}
							}

						}

					}
				}
			}
		}

		.fn {
			padding: 20px 15px 0;

			.btn {
				width: 100%;
				padding: 18px 0;
				text-align: center;
				margin-bottom: 15px;
				border-radius: 5px;
			}

			.confirm {
				background-color: #0C80FB;
				color: #fff;
			}

			.sele {
				background-color: #fff;
				color: #000;
			}
			.msg2 {
				text-align: center;
				font-size: 15px;
				color: #999999;
				margin-bottom: 20px;
				display: flex;
				align-items: center;
				
				span {
					color: #0c80fb;
				}
			}

			.msg {
				text-align: center;
				font-size: 15px;
				color: #999999;

				span {
					color: #0C80FB;
				}
			}
		}
	}
</style>